package com.simple.admin.controller.auth;

import com.alibaba.fastjson2.JSON;
import com.simple.auth.domain.dto.AuthParamsDto;
import com.simple.auth.domain.dto.AuthSendSmsDto;
import com.simple.auth.domain.dto.RefreshTokenDto;
import com.simple.auth.domain.vo.AuthSuccessVO;
import com.simple.auth.domain.vo.CaptchaVo;
import com.simple.auth.service.AuthService;
import com.simple.auth.service.CaptchaService;
import com.simple.auth.service.RefreshTokenService;
import com.simple.auth.service.SmsService;
import com.simple.common.exception.ServiceException;
import com.simple.framework.aspect.Encrypt;
import com.simple.framework.core.controller.BaseController;
import com.simple.framework.core.domain.R;
import com.simple.framework.limit.Limit;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 系统认证控制器
 * @Author xiaoke
 * @Date 2024/8/10 18:00
 * @Version 1.0
 */
@RequestMapping("auth")
@RestController
public class AuthController extends BaseController {

    private final ApplicationContext applicationContext;
    private final SmsService smsService;

    private final CaptchaService captchaService;

    public AuthController(ApplicationContext applicationContext, SmsService smsService, CaptchaService captchaService) {
        this.applicationContext = applicationContext;
        this.smsService = smsService;
        this.captchaService = captchaService;
    }


    /**
     * 系统统一登录
     * @param dto 登录参数
     * @return AuthSuccessVO
     */
    @Encrypt
    @PostMapping("/login")
    public Object login(@RequestBody @Validated AuthParamsDto dto) {
        AuthService bean = null;
        try {
            bean = (AuthService) applicationContext.getBean(dto.getAuthType() + "_auth");
        } catch (BeansException e) {
            throw new ServiceException("未找到对应的认证器");
        }
        return JSON.toJSONString(bean.login(dto));
    }

    /**
     * 刷新token
     * @param dto 刷新token参数
     * @return AuthSuccessVO
     */
    @PostMapping("/refresh")
    public R<AuthSuccessVO> refresh(@RequestBody @Validated RefreshTokenDto dto) {
        RefreshTokenService bean = null;
        try {
            bean = (RefreshTokenService) applicationContext.getBean(dto.getAuthType() + "_auth");
        } catch (Exception e) {
            throw new ServiceException("未找到对应的认证器");
        }
        return success(bean.refresh(dto));
    }

    /**
     * 发送短信验证码
     * @param dto 短信参数
     * @return Boolean
     */
    @PostMapping("/sendSms")
    public R<Boolean> sendSms(@RequestBody @Validated AuthSendSmsDto dto) {
        return success(smsService.sendVerificationCode(dto.getPhone()));
    }

    /**
     * 获取验证码
     * @return CaptchaVo
     */
    @Limit(key = "auth-code",permitsPerSecond = 1,timeout = 1000)
    @PostMapping("/code")
    public R<CaptchaVo> code() {
        return success(captchaService.generated());
    }

}
